import logging.config
import time

from selenium import webdriver
from selenium.webdriver.common.service import logger
from selenium.webdriver.remote.remote_connection import LOGGER
from urllib3.connectionpool import log

from common import options
from settings import log_dir, root_dir
from utils.models import BrowserName

POLL_FREQUENCY: float = 0.5
# 屏蔽Selenium的日志功能
log.setLevel(logging.WARNING)
logger.setLevel(logging.WARNING)
LOGGER.setLevel(logging.WARNING)


def get_driver(browser_name: str = 'Chrome'):
    """

    :param browser_name:
    :return:
    :rtype: webdriver.Remote
    """
    for browser in BrowserName:
        if browser_name in browser.value:
            browser_type = browser.name
            break
    else:
        browser_type = 'Chrome'

    klass = getattr(webdriver, browser_type)
    opt_type = browser_type.lower() + '_options'
    opt_func = getattr(options, opt_type)
    return klass(options=opt_func())


def init_logging():
    """初始化logging"""
    log_dir.mkdir(parents=True, exist_ok=True)
    logging.__logfile__ = log_dir / f"{time.strftime('%Y-%m-%d %H-%M-%S')}.log"
    logging.config.fileConfig(root_dir / 'config' / 'logging.ini')
